/*
 * Copyright (c) 2015 Uwantsoft Ltd.
 * www.uwantsoft.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Uwantsoft ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with Uwantsoft.
 */

package com.uwantsoft.business.infrastructure.repository;

import com.uwantsoft.business.facade.dto.user.UserOverviewQueryCondition;
import com.uwantsoft.business.infrastructure.repository.querier.UserOverviewQuerier;
import com.uwantsoft.business.service.domain.user.Driver;
import com.uwantsoft.business.service.domain.user.User;
import com.uwantsoft.business.service.infrastructure.UserRepository;
import com.uwantsoft.util.HibernateEntityRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Chuanbao
 */
@SuppressWarnings("unchecked")
@Repository(value = "hibernateUserRepository")
public class HibernateUserRepository extends HibernateEntityRepository implements UserRepository {

    public User findByUsername(String username) {
        List<User> users = (List<User>) getHibernateTemplate().find("from User u where u.active=true and u.username=?", username);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public User findUser(String guid) {
        List<User> users = (List<User>) getHibernateTemplate().find("from User u where u.guid = ?", guid);
        return users.isEmpty() ? null : users.get(0);
    }

    @Override
    public List<User> findUsers(final UserOverviewQueryCondition condition) {
        UserOverviewQuerier userOverviewQuerier = new UserOverviewQuerier(getHibernateTemplate(),condition);
        return userOverviewQuerier.getResults();
    }

    @Override
    public int findUserTotalAmount(final UserOverviewQueryCondition condition) {
        UserOverviewQuerier userOverviewQuerier = new UserOverviewQuerier(getHibernateTemplate(),condition);
        return userOverviewQuerier.getAmount();
    }
}
